package com.theobserver.theobserver;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wssxk on 7/8/17.
 * 热点新闻的Fragment
 */

public class NewsHotFragment extends Fragment {

    FragmentManager fm = null;
    Context mContext = null;
    ArrayList<AVObject> mArray = null;
    PopupMenu catalog_menu = null;
    ViewPager vp = null;
    //PopupWindow search_popup = null;

    public void init(FragmentManager fragmentManager, Context context)
    {
        fm = fragmentManager;
        mContext = context;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
    {
        View v = inflater.inflate(R.layout.main_page_news_list_layout, container, false);

        vp = (ViewPager) v.findViewById(R.id.viewpage_main_page_hot);
        final MainPageHotNewsPageAdapter page_adapter = new MainPageHotNewsPageAdapter(fm);
        page_adapter.init(mContext);


        /*
        //设置弹出窗口
        final RelativeLayout hot_page_head_bar = v.findViewById(R.id.relativelayout_hot_page_headbar);
        search_popup = new PopupWindow(mContext);
        View search_popup_layout = View.inflate(mContext, R.layout.popup_search_view, null);
        search_popup.setContentView(search_popup_layout);
        search_popup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        search_popup.setHeight(150);
        //search_popup.
        */

        final DrawerLayout drawer =  ((FragmentActivity)mContext).findViewById(R.id.main_page_drawer);
        ImageView imageview_slider = (ImageView) v.findViewById(R.id.imageview_hotpage_navi_slider);
        imageview_slider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(Gravity.LEFT);
            }
        });



        ImageView imageview_search = v.findViewById(R.id.imageview_hot_page_search);
        imageview_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //search_popup.showAtLocation(hot_page_head_bar, Gravity.NO_GRAVITY, 0, 0);
                startActivity(new Intent(mContext, SearchActivity.class));
            }
        });

        //分页适配器
        vp.setAdapter(page_adapter);
        TabLayout tl = (TabLayout) v.findViewById(R.id.tablayout_main_page_hot);
        tl.setTabMode(TabLayout.MODE_SCROLLABLE);
        //tl.setTabsFromPagerAdapter(page_adapter);
        tl.setupWithViewPager(vp);

        //修改显示的分类
        ImageView catalog_add = (ImageView) v.findViewById(R.id.imageview_catalog_select);
        catalog_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(mContext, CatalogSelectActivity.class));
                catalog_menu.show();
            }
        });

        catalog_menu = new PopupMenu(mContext, catalog_add);
        catalog_menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                vp.setCurrentItem(menuItem.getItemId());
                //Toast.makeText(mContext, menuItem.getItemId()+"", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        AVQuery<AVObject> catalog_query = new AVQuery<>("catalog");
        catalog_query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e != null)
                {
                    Toast.makeText(mContext, "get catalog :" + e.toString(), Toast.LENGTH_SHORT).show();
                }
                else
                {
                    mArray = new ArrayList<AVObject>();
                    Menu t_menu = catalog_menu.getMenu();
                    t_menu.add(Menu.NONE, 0, Menu.NONE, "推荐");
                    for(int t = 0; t < list.size(); t ++) {
                        mArray.add(list.get(t));
                        t_menu.add(Menu.NONE, t+1, Menu.NONE, list.get(t).getString("catalog_name"));   //将分类添加到结果中去
                    }

                    AVObject catalog_recommand = new AVObject("catalog");
                    catalog_recommand.put("catalog_name", "推荐");
                    page_adapter.catalog.add(catalog_recommand);

                    page_adapter.catalog.clear();
                    page_adapter.catalog.add(catalog_recommand);
                    page_adapter.catalog.addAll(mArray);
                    page_adapter.notifyDataSetChanged();
                }
            }
        });



        return v;
    }

}
